﻿namespace HRM.CORE
{
    using DevExpress.Utils;
    using DevExpress.XtraBars;
    using DevExpress.XtraEditors;
    using DevExpress.XtraGrid.Views.BandedGrid;
    using APP.HELPER;
    using Common.Class; using APP.HELPER.Utils.Language; using Security.Security; using Dictionary.ERP;
    using HRM.CORE;
    using System; using Extra.ERP;
    using System.ComponentModel;
    using System.Windows.Forms; using Common;
    using Security.ERP;

    public class xucRecruitment : Common.xucBaseBasic
    {
        private IContainer components = null;

        public xucRecruitment()
        {
            this.InitializeComponent();
            base.ucToolBar.SetInterface();
        }

        protected override void Add()
        {
            xfmRecruitmentAdd add = new xfmRecruitmentAdd(Actions.Add);
            add.Added += new xfmRecruitmentAdd.AddedEventHander(this.frm_Added);
            add.ShowDialog();
        }

        private void AddRow(HRM_RECRUITMENT Item)
        {
            AdvBandedGridView gbList = base.gbList;
            int focusedRowHandle = gbList.FocusedRowHandle;
            gbList.AddNewRow();
            focusedRowHandle = gbList.FocusedRowHandle;
            gbList.SetRowCellValue(focusedRowHandle, "RecruitmentCode", Item.RecruitmentCode);
            gbList.SetRowCellValue(focusedRowHandle, "RecruitmentName", Item.RecruitmentName);
            gbList.SetRowCellValue(focusedRowHandle, "FromDate", Item.FromDate);
            gbList.SetRowCellValue(focusedRowHandle, "ToDate", Item.ToDate);
            gbList.SetRowCellValue(focusedRowHandle, "DecideNumber", Item.DecideNumber);
            gbList.SetRowCellValue(focusedRowHandle, "FilePath", Item.FilePath);
            gbList.SetRowCellValue(focusedRowHandle, "Status", Item.Status);
            gbList.SetRowCellValue(focusedRowHandle, "Description", Item.Description);
            gbList.UpdateCurrentRow();
        }

        public override void Change()
        {
            HRM_RECRUITMENT item = new HRM_RECRUITMENT();
            object cellValue = base.GetCellValue(base.m_RowClickEventArgs.RowIndex, "RecruitmentCode");
            if (cellValue != null)
            {
                base.SetWaitDialogCaption("Đang kiểm tra dữ liệu....");
                if (item.Get(cellValue.ToString()) != "OK")
                {
                    this.DoHide();
                    XtraMessageBox.Show("Dữ liệu kh\x00f4ng tồn tại", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    this.DoHide();
                    xfmRecruitmentAdd add = new xfmRecruitmentAdd(Actions.Update, item);
                    add.Updated += new xfmRecruitmentAdd.UpdatedEventHander(this.frm_Updated);
                    add.Added += new xfmRecruitmentAdd.AddedEventHander(this.frm_Added);
                    add.ShowDialog();
                }
            }
        }

        public override void Delete()
        {
            if (!ClsOption.System2.IsQuestion || (XtraMessageBox.Show("Bạn c\x00f3 muốn x\x00f3a kh\x00f4ng?", "Th\x00f4ng b\x00e1o", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.No))
            {
                object cellValue;
                base.SetWaitDialogCaption("Đang x\x00f3a...");
                string str = "";
                bool flag = false;
                AdvBandedGridView gbList = base.gbList;
                int[] selectedRows = gbList.GetSelectedRows();
                HRM_RECRUITMENT hrm_recruitment = new HRM_RECRUITMENT();
                for (int i = selectedRows.Length; i > 0; i--)
                {
                    flag = true;
                    cellValue = base.GetCellValue(selectedRows[i - 1], "RecruitmentCode");
                    if (cellValue != null)
                    {
                        SYS_LOG.Insert("Đợt Tuyển Dụng", "Xo\x00e1", cellValue.ToString());
                        str = hrm_recruitment.Delete(cellValue.ToString());
                        if (str == "OK")
                        {
                            gbList.DeleteRow(selectedRows[i - 1]);
                        }
                        else if (str != "OK")
                        {
                            MessageBox.Show("Th\x00f4ng tin kh\x00f4ng được x\x00f3a\n" + str, "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        }
                    }
                }
                this.DoHide();
                if (!flag && (gbList.DataSource != null))
                {
                    RowClickEventArgs args = new RowClickEventArgs((gbList == null) ? -1 : gbList.FocusedRowHandle, (gbList.FocusedColumn == null) ? -1 : gbList.FocusedColumn.ColumnHandle, (gbList.FocusedColumn == null) ? "" : gbList.FocusedColumn.FieldName);
                    base.m_RowClickEventArgs = args;
                    cellValue = null;
                    cellValue = base.GetCellValue(args.RowIndex, "RecruitmentCode");
                    if (cellValue != null)
                    {
                        SYS_LOG.Insert("Đợt Tuyển Dụng", "Xo\x00e1", cellValue.ToString());
                        base.SetWaitDialogCaption("Đang x\x00f3a...");
                        str = hrm_recruitment.Delete(cellValue.ToString());
                        if (str == "OK")
                        {
                            gbList.DeleteRow(args.RowIndex);
                        }
                        else if (str != "OK")
                        {
                            MessageBox.Show("Th\x00f4ng tin kh\x00f4ng được x\x00f3a\n" + str, "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        }
                        this.DoHide();
                    }
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        protected override bool ExportPermision()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiRecruitment") != "OK")
            {
                return false;
            }
            if (!MyRule.AllowExport)
            {
                MyRule.Notify();
                return false;
            }
            SYS_LOG.Insert("Đợt Tuyển Dụng", "Xuất");
            return base.ExportPermision();
        }

        private void frm_Added(object sender, HRM_RECRUITMENT e)
        {
            this.AddRow(e);
        }

        private void frm_Updated(object sender, HRM_RECRUITMENT Item)
        {
            this.UpdateRow(Item, base.m_RowClickEventArgs);
        }

        protected override void gbList_MouseDown(object sender, MouseEventArgs e)
        {
            AdvBandedGridView view = (AdvBandedGridView) sender;
            RowClickEventArgs args = new RowClickEventArgs((view == null) ? -1 : view.FocusedRowHandle, (view.FocusedColumn == null) ? -1 : view.FocusedColumn.ColumnHandle, (view.FocusedColumn == null) ? "" : view.FocusedColumn.FieldName);
            base.m_RowClickEventArgs = args;
            object cellValue = base.GetCellValue(args.RowIndex, "RecruitmentCode");
            base.DisableMenu(false);
            if (cellValue == null)
            {
                base.DisableMenu(true);
            }
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            base.AutoScaleMode = AutoScaleMode.Font;
        }

        protected override void List_Init(AdvBandedGridView dt)
        {
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                switch (dt.Columns[i].FieldName)
                {
                    case "RecruitmentCode":
                        dt.Columns[i].OptionsColumn.ReadOnly = true;
                        dt.Columns[i].OptionsColumn.AllowEdit = false;
                        dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.FixedWidth = true;
                        if (SysOption.Language == "VN")
                        {
                            dt.Columns[i].Caption = "M\x00e3";
                        }
                        dt.Columns[i].Width = 80;
                        break;

                    case "RecruitmentName":
                        dt.Columns[i].OptionsColumn.ReadOnly = true;
                        dt.Columns[i].OptionsColumn.AllowEdit = false;
                        dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.FixedWidth = true;
                        if (SysOption.Language == "VN")
                        {
                            dt.Columns[i].Caption = "T\x00ean";
                        }
                        dt.Columns[i].Width = 180;
                        break;

                    case "FromDate":
                        dt.Columns[i].OptionsColumn.ReadOnly = true;
                        dt.Columns[i].OptionsColumn.AllowEdit = false;
                        dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.FixedWidth = true;
                        if (SysOption.Language == "VN")
                        {
                            dt.Columns[i].Caption = "Từ ng\x00e0y";
                        }
                        dt.Columns[i].Width = 100;
                        break;

                    case "ToDate":
                        dt.Columns[i].OptionsColumn.ReadOnly = true;
                        dt.Columns[i].OptionsColumn.AllowEdit = false;
                        dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.FixedWidth = true;
                        if (SysOption.Language == "VN")
                        {
                            dt.Columns[i].Caption = "Đến ng\x00e0y";
                        }
                        dt.Columns[i].Width = 100;
                        break;

                    case "DecideNumber":
                        dt.Columns[i].OptionsColumn.ReadOnly = true;
                        dt.Columns[i].OptionsColumn.AllowEdit = false;
                        dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.FixedWidth = true;
                        if (SysOption.Language == "VN")
                        {
                            dt.Columns[i].Caption = "Quyết định";
                        }
                        dt.Columns[i].Width = 120;
                        break;

                    case "FilePath":
                        dt.Columns[i].OptionsColumn.ReadOnly = true;
                        dt.Columns[i].OptionsColumn.AllowEdit = false;
                        dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.FixedWidth = true;
                        if (SysOption.Language == "VN")
                        {
                            dt.Columns[i].Caption = "Đường dẫn";
                        }
                        dt.Columns[i].Width = 200;
                        break;

                    case "Status":
                        dt.Columns[i].OptionsColumn.ReadOnly = true;
                        dt.Columns[i].OptionsColumn.AllowEdit = false;
                        dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.FixedWidth = true;
                        if (SysOption.Language == "VN")
                        {
                            dt.Columns[i].Caption = "Trạng th\x00e1i";
                        }
                        dt.Columns[i].Width = 120;
                        break;

                    case "Description":
                        dt.Columns[i].OptionsColumn.ReadOnly = true;
                        dt.Columns[i].OptionsColumn.AllowEdit = false;
                        dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.FixedWidth = true;
                        if (SysOption.Language == "VN")
                        {
                            dt.Columns[i].Caption = "Ghi ch\x00fa";
                        }
                        dt.Columns[i].Width = 150;
                        break;

                    default:
                        dt.Columns[i].Visible = false;
                        break;
                }
            }
        }

        protected override void Print()
        {
            SYS_LOG.Insert("Đợt Tuyển Dụng", "In");
            base.Print();
        }

        public override void ReLoad()
        {
            base.SetWaitDialogCaption("Đang nạp dữ liệu...");
            base.gcList.DataSource = new HRM_RECRUITMENT().GetList();
            base.SetWaitDialogCaption("Đang nạp cấu h\x00ecnh...");
            this.List_Init(base.gbList);
            base.SetWaitDialogCaption("Nạp quyền sử dụng...");
            MyRule.Get(MyLogin.RoleId, "bbiRecruitment");
            if (!MyRule.AllowPrint)
            {
                base.ucToolBar.bbiPrint.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiRecruitment");
            if (!MyRule.AllowExport)
            {
                base.ucToolBar.bbiExport.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiRecruitment");
            if (!MyRule.AllowAdd)
            {
                base.ucToolBar.bbiAdd.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiRecruitment");
            if (!MyRule.AllowDelete)
            {
                base.ucToolBar.bbiDelete.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiRecruitment");
            if (!MyRule.AllowEdit)
            {
                base.ucToolBar.bbiEdit.Visibility = BarItemVisibility.Never;
            }
            base.SetWaitDialogCaption("Đ\x00e3 xong...");
            this.DoHide();
        }

        private void UpdateRow(HRM_RECRUITMENT item, RowClickEventArgs e)
        {
            AdvBandedGridView gbList = base.gbList;
            int rowIndex = e.RowIndex;
            gbList.SetRowCellValue(rowIndex, "RecruitmentCode", item.RecruitmentCode);
            gbList.SetRowCellValue(rowIndex, "RecruitmentName", item.RecruitmentName);
            gbList.SetRowCellValue(rowIndex, "FromDate", item.FromDate);
            gbList.SetRowCellValue(rowIndex, "ToDate", item.ToDate);
            gbList.SetRowCellValue(rowIndex, "DecideNumber", item.DecideNumber);
            gbList.SetRowCellValue(rowIndex, "FilePath", item.FilePath);
            gbList.SetRowCellValue(rowIndex, "Status", item.Status);
            gbList.SetRowCellValue(rowIndex, "Description", item.Description);
            gbList.UpdateCurrentRow();
        }
    }
}

